/**
 * 
 */
package vn.smartware.co.framework.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * PropertyResources
 */
@SuppressWarnings("rawtypes")
public class PropertyResources {
	
	private static Map map = new HashMap();
	
	/**
	 * @param filePath
	 * @return Properties
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static Properties getProperties(String filePath) 
	                                           throws IOException {
		
		Properties prop = (Properties) map.get(filePath);
		
		if (prop != null) {
			return prop;
		}
		
		synchronized (PropertyResources.class) {
			prop = (Properties) map.get(filePath);
			
			if (prop != null) {
				return prop;
			}
		}
		
		prop = new Properties();
		File file = new File(filePath);
		prop.load(new FileInputStream(file));
		map.put(filePath, prop);
		
		return prop;
	}
	
	/**
	 * @param fileName
	 * @param is
	 * @return Properties
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static Properties getProperties(String fileName, InputStream is) 
	                                             throws IOException {
		
		Properties prop = (Properties) map.get(fileName);
		
		if (prop != null) {
			return prop;
		}
		
		synchronized (PropertyResources.class) {
			prop = (Properties) map.get(fileName);
			
			if (prop != null) {
				return prop;
			}
		}
		
		prop = new Properties();
		prop.load(is);
		map.put(fileName, prop);
		
		return prop;
	}
}
